<?php

header("Content-Type: text/html;charset=utf8");

class BbsDb {

    private $mysql;

    public function __construct(InMysql $mysql = null) {
        if ($mysql == null) {
            $this->mysql = new InMysql();
        } else {
            $this->mysql = $mysql;
        }
    }

    public function listBbs($type = 0) {
        if ($type == 0) {
            $sql = "select * from bbs ORDER by top desc,date DESC";
        } else {
            $sql = "select * from bbs where type = " . $type . " ORDER by top desc,date DESC";
        }
        return $this->mysql->fetchData($sql);
    }

    public function addBbs($content, $title, $type) {
        $sql = "insert into bbs(date,content,title,type) values(UNIX_TIMESTAMP(),'" . $content . "','" . $title . "','" . $type . "')";
        return $this->mysql->insertData($sql);
    }

    public function editBbs($id, $content, $title, $type) {
        $sql = "update bbs set content = '" . $content . "',title = '" . $title . "',type = '" . $type . "' where id = " . $id;
        return $this->mysql->executeData($sql);
    }

    public function deleteBbs($id) {
        $sql = "delete from bbs where id = " . $id;
        return $this->mysql->executeData($sql);
    }

    public function topBbs($id, $top) {
        $sql = "update bbs set top = '" . $top . "' where id = " . $id;
        return $this->mysql->executeData($sql);
    }

}

?>